﻿using CardPlay.Model.CardModel;
using CardPlay.Model.GameModel;
using CardPlay.Model.GameModel.Actions;
using CardPlay.Model.GameModel.Presentation;
using CardPlay.Model.Layout;

namespace CardPlay.DemoPack.Games.SirTommyVariants
{
    [Game("330E8489-EAEB-42F2-91C6-6E8471A75D0F", LevelOfDifficulty.Easy, LevelOfSkill.ModerateSkill, DurationOfPlay.LessThan5Minutes, Family = FamilyName)]
    public class SirTommy : StandardCardGame
    {
        public const string FamilyName = "Sir Tommy";

        public SirTommy()
        {
            MaximumDeals = 1;

            NamesOfAdditionalPiles.Add(Stock);

            TotalFoundations = 4;
            TotalTableaus = 4;
            TableauBuildSequence = CardFaceSequence.AnySuitAnyRank;
        }

        public override ILayoutSite CreatePresentation(IPresentationContext presentation)
        {
            var root = presentation.CreateRowLayout();
            root.Add(presentation.CreatePileVisual(Stock, presentation.StyleForStock(this)));

            var column = presentation.CreateColumnLayout();
            root.Add(column);

            var row1 = presentation.CreateRowLayout();
            column.Add(row1);
            row1.HorizontalContentAlignment = HorizontalAlignment.Center;
            presentation.AddFoundationPiles(this, row1, FoundationBaseRank);

            var row2 = presentation.CreateRowLayout();
            column.Add(row2);
            presentation.AddTableauPiles(this, row2);

            return root;
        }

        protected override bool AllowedToPickupAtTableau(RemovePileAction action)
        {
            return action.IsActingOnFaceUpTopCard();
        }

        protected override bool AllowedToBuildEmptyTableau(MovePileAction action)
        {
            return action.SourcePile.Location.Matches(Stock) && action.PileBeingMoved.BottomCard.Rank != FoundationBaseRank;
        }

        protected override bool AllowedToBuildNonEmptyTableau(MovePileAction action)
        {
            return 
                base.AllowedToBuildNonEmptyTableau(action) &&
                action.PileBeingMoved.BottomCard.Rank != FoundationBaseRank && !action.SourcePile.Location.Matches(AnyTableau);
        }

        protected override void PrepareDealScript(DealScript script)
        {
            script.DealRemainingFaceUpTo(Stock);
        }
    }
}
